//
//  DataBaseHandle.m
//  Alltelevisionnews
//
//  Created by dllo on 15/12/5.
//  Copyright © 2015年 三人行. All rights reserved.
//

#import "DataBaseHandle.h"

@implementation DataBaseHandle

+ (instancetype)shareDataBasaHandle {
    
    static DataBaseHandle *handle;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[DataBaseHandle alloc] init];
    });
    [handle openDB];
    return handle;
}

- (void)openDB {
    
    // 1 沙盒路径
    
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES) lastObject];
    NSString *dbPath = [sandBoxPath stringByAppendingPathComponent:@"News.sqlite"];
    int result = sqlite3_open([dbPath UTF8String], &dbpoint);
    
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    } else {
        NSLog(@"打开失败");
    }
    
    NSLog(@"%@", dbPath);
}

// 创建数据库
- (void)createTable {
    
    //    NSString *sqlStr = @"create table if not exists CLguidang (productId text, title text, image text)";
    NSString *sqlStr = @"create table if not exists baseDataModel (tempId, title, image)";
    
    // 执行语句
    int result = sqlite3_exec(dbpoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"创建成功");
    } else {
        NSLog(@"创建失败");
    }
    //
}

// 查询有无重复
-(SelectbaseDataModel)isHaveInTable:(baseDataModel *)model{
    // 除了openDB 都先写sql语句
    //    NSString *sqlStr = [NSString stringWithFormat:@"select * from guidang where productId = '%@' and flag = '1'", guidang.productId];
    NSString *sqlStr = [NSString stringWithFormat:@"select * from baseDataModel where tempId = '%@'", model.tempId];
    // 跟随指针
    sqlite3_stmt *stmt = nil;
    // 执行sql语句
    int result = sqlite3_prepare_v2(dbpoint, [sqlStr UTF8String], -1, &stmt, nil);
    NSLog(@"%d", result);
    if (result == SQLITE_OK) {
        NSLog(@"查询成功");
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            sqlite3_finalize(stmt);
            // 在
            return Intable;
        } else {
            sqlite3_finalize(stmt);
            // 不在
            return NotInTable;
        }
    } else {
        sqlite3_finalize(stmt);
        NSLog(@"查询失败");
        
        return SelectError;
    }
    
}

//// 收藏
- (void)saveNewsInDB:(baseDataModel *)model {
    
    NSString *sqlStr = [NSString stringWithFormat:@"insert into baseDataModel (tempId, title, image) values ('%@', '%@', '%@')", model.tempId, model.title, model.image];
    
    int result = sqlite3_exec(dbpoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"添加成功");
    }
    else {
        NSLog(@"添加失败");
    }
}

- (void)deleteNewsInDB:(baseDataModel *)model {
    
    NSString *sqlStr = [NSString stringWithFormat:@"delete from baseDataModel where tempId = '%@'", model.tempId];
    
    int result = sqlite3_exec(dbpoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }
    else {
        NSLog(@"删除失败");
    }
}

- (NSMutableArray *)selectArrNews {
    NSMutableArray *newsArr = [NSMutableArray array];
    
    NSString *sqlStr = @"select * from baseDataModel";
    
    sqlite3_stmt *stmt = nil;
    
    // 执行查找
    int result = sqlite3_prepare_v2(dbpoint, [sqlStr UTF8String], -1, &stmt, nil);
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            baseDataModel *new = [[baseDataModel alloc] init];
            new.title = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 1)];
            new.tempId = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 0)];
            new.image = [NSString stringWithUTF8String:(const char *) sqlite3_column_text(stmt, 2)];
            [newsArr addObject:new];
            
        }
    }
    sqlite3_finalize(stmt);
    return newsArr;
}



@end
